package com.myrecycleview631wrydata.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cst.a613ld.recycleview613ld.R;
import cst.a613ld.recycleview613ld.activity.KeJianDetailActivity;
import cst.a613ld.recycleview613ld.adapter.ArticleAdapter;
import cst.a613ld.recycleview613ld.adapter.MyAdapter;
import cst.a613ld.recycleview613ld.bean.ArticleBean;
import cst.a613ld.recycleview613ld.iface.ArticleListener;
import cst.a613ld.recycleview613ld.model.ArticleModel;

/**
 * Created by 64410 on 2017/5/23.
 */

public class KejianFragment extends Fragment{
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;
    List<ArticleBean> list = null;
    private View view;
    private Context context;
    @Nullable
    ArticleListener articleListener = new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> listbean) {
            list = listbean;
            myAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view=inflater.inflate(R.layout.kejianfragment,container,false);
        context=getActivity();
        initRecyclerView();
        ArticleModel articleModel =new ArticleModel();
        articleModel.getResultList("video",2,"j4qb154rhgpmphjqfvh978lem7",articleListener);
        return view;
    }
    //江湖的尽头，是否只剩孤独。
        private void initRecyclerView()
    {
        recyclerView = (RecyclerView)view.findViewById(R.id.recycleview);
//        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(context);    //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);    //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);    //创建并设置Adapter
//        //list = getData();
        myAdapter = new MyAdapter(context);
        ArticleAdapter.OnItemClickListener onItemClickListener= new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), KeJianDetailActivity.class);
                intent.putExtra("resid",list.get(position).getId());
                    startActivity(intent);
            }
        };
        //myAdapter.setOnItemClickListenrer(onItemClickListener);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
    }
}
